package com.yuelan.codelib.download;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.RemoteViews;

import com.exmaple.baidumv.comm.App;

public class TpNfView {
	private static NotificationManager manager;
	private Context c;
	private Notification notification_jd;

	public TpNfView(Context c) {
		this.c = c;
		manager = (NotificationManager) c
				.getSystemService(c.NOTIFICATION_SERVICE);
	}

	public void showDownloadView(String filename, int samllicon, Bitmap icon,
			String filesize, int jindu, String sudu, int nid,
			PendingIntent pendingIntent, boolean canCanel) {
		if (notification_jd == null) {
			if (samllicon == 0) {
				notification_jd = new Notification(
						android.R.drawable.stat_sys_download, filename
								+ "任务开始..", System.currentTimeMillis());
			} else {
				notification_jd = new Notification(samllicon, filename
						+ "任务开始..", System.currentTimeMillis());
			}
		}
		RemoteViews mRemoteView = new RemoteViews(c.getPackageName(),
				App.getIdByName(c, "layout", "dm_download_notify"));
		notification_jd.contentView = mRemoteView;
		notification_jd.contentView.setTextViewText(
				App.getIdByName(c, "id", "dm_downloadntview_filename"),
				filename);
		notification_jd.contentView.setTextViewText(
				App.getIdByName(c, "id", "dm_downloadntview_filesize"),
				filesize);
		notification_jd.contentView.setTextViewText(
				App.getIdByName(c, "id", "dm_downloadntview_precent"), jindu
						+ "%");
		if (sudu.equals("0")) {
			notification_jd.contentView
					.setTextViewText(App.getIdByName(c, "id",
							"dm_downloadntview_networksd"), "");
		} else {
			notification_jd.contentView.setTextViewText(
					App.getIdByName(c, "id", "dm_downloadntview_networksd"),
					sudu);
		}
		if (icon != null) {
			notification_jd.contentView.setImageViewBitmap(
					App.getIdByName(c, "id", "dm_downloadntview_icon"), icon);
		} else {
			notification_jd.contentView.setImageViewResource(
					App.getIdByName(c, "id", "dm_downloadntview_icon"),
					c.getApplicationInfo().icon);
		}
		notification_jd.contentView.setProgressBar(
				App.getIdByName(c, "id", "dm_downloadntview_progressBar"), 100,
				jindu, false);
		if (canCanel) {
			notification_jd.flags = Notification.FLAG_AUTO_CANCEL;
		} else {
			notification_jd.flags = Notification.FLAG_NO_CLEAR;
		}
		if (pendingIntent != null) {
			notification_jd.contentIntent = pendingIntent;
		} else {
			Intent intent = new Intent(
					"com.dm.demo.action.DownloadNotifyView.null");
			pendingIntent = PendingIntent.getBroadcast(c, 0, intent,
					PendingIntent.FLAG_UPDATE_CURRENT);
			notification_jd.contentIntent = pendingIntent;
		}
		manager.notify(nid, notification_jd);
	}
	
	public void closeDownloadView(int nid)
	{
		manager.cancel(nid);
	}
}
